package org.jeecg.modules.renyGj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.hkvs.HaikService;
import org.jeecg.modules.hkvs.HaikYitstjg;
import org.jeecg.modules.hkvs.HaikZhuaPData;
import org.jeecg.modules.renldmHuod.entity.AnRenldmHuod;
import org.jeecg.modules.renldmHuod.service.IAnRenldmHuodService;
import org.jeecg.modules.renldmHuodSign.entity.AgRenldmHuodSign;
import org.jeecg.modules.renldmHuodSign.service.IAgRenldmHuodSignService;
import org.jeecg.modules.renyGj.entity.AgRenygj;
import org.jeecg.modules.renyGj.mapper.AgRenygjMapper;
import org.jeecg.modules.renyGj.service.IAgRenygjService;
import org.jeecg.modules.renyGj.vo.RenygjxlGuaidVO;
import org.jeecg.modules.renyGj.vo.Renyjj;
import org.jeecg.modules.renygjxl.entity.Renygjxl;
import org.jeecg.modules.renygjxl.entity.RenygjxlGuaid;
import org.jeecg.modules.renygjxl.service.IRenygjxlGuaidService;
import org.jeecg.modules.renygjxl.service.IRenygjxlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 人员轨迹
 * @Author: jeecg-boot
 * @Date:   2020-10-27
 * @Version: V1.0
 */
@Service
public class AgRenygjServiceImpl extends ServiceImpl<AgRenygjMapper, AgRenygj> implements IAgRenygjService {

    @Autowired
    private IRenygjxlGuaidService renygjxlGuaidService;

    @Autowired
    private IRenygjxlService renygjxlService;

    @Autowired
    private IAgRenygjService agRenygjService;

    @Autowired
    HaikService haikService;

    @Autowired
    private IAgRenldmHuodSignService agRenldmHuodSignService;

    @Autowired
    private IAnRenldmHuodService anRenldmHuodService;

    @Override
    public List renygjByName(String name) {
        return this.getBaseMapper().renygjByName(name);
    }

    @Override
    public List<Renyjj> renyjjgj(String kaissj, String jiessj, String faz) {
        return this.getBaseMapper().renyjjgj(kaissj, jiessj, faz);
    }

    boolean yetFinishSync = false;
    boolean testIsExist(AgRenygj agRenygj) {
        AgRenygj result = agRenygjService.getOne(new QueryWrapper<AgRenygj>()
//                .eq("xingm", agRenygj.getXingm())
                .eq("zhuapsj", agRenygj.getZhuapsj())
                .eq("zhuapsbbh", agRenygj.getZhuapsbbh()), false);

        if(result != null)
            return true;
        else
            return false;
    }
    @Override
    public Result<?> syncData() {
        // 如果上次没同步完，直接返回
        if (yetFinishSync) {
            System.out.println("yet over");
            return null;
        }

        yetFinishSync = true;

        String startTime = "2020-01-01T00:00:00.000+08:00";
        AgRenygj agRenygj2 = agRenygjService.getOne(new QueryWrapper<AgRenygj>().orderByDesc("zhuapsj"), false);

        if(agRenygj2 != null) {
            System.out.println(DateUtils.formatTime(agRenygj2.getZhuapsj()));
            startTime = DateUtils.date2Str(agRenygj2.getZhuapsj(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'"));
        }

        String endTime = DateUtils.date2Str(new Date(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'"));

        List<AgRenygj> list2 = new ArrayList<>();
        List<HaikZhuaPData> list = haikService.face_captureSearch(startTime, endTime);
        list.stream().forEach(e -> {
            AgRenygj agRenygj = new AgRenygj();

            agRenygj.setShibzp(e.getSnapUrl());
            HaikYitstjg haikYitstjg = haikService.oneToMany(e.getSnapUrl());
            if(haikYitstjg != null) {
                agRenygj.setXingm(haikYitstjg.getFaceInfo().getName());
                agRenygj.setRenybh(haikYitstjg.getFaceInfo().getCertificateNum());
            }

            agRenygj.setZhuapbj(e.getBkgUrl());

            agRenygj.setZhuapsj(e.getEventTime());
            agRenygj.setCreateTime(e.getEventTime());

            agRenygj.setZhuapsbmc(e.getCameraName());
            agRenygj.setZhuapsbbh(e.getCameraIndexcode());

            if(e.getGender().equals("male"))
                agRenygj.setXingb("1");
            else
                agRenygj.setXingb("2");

            if(e.getClass().equals("yes"))
                agRenygj.setShifdyj("1");
            else
                agRenygj.setShifdyj("0");

            agRenygj.setNianl(e.getAge());

            if(!testIsExist(agRenygj)) {
                agRenygjService.save(agRenygj);

                // 查找激活的好活动
                AnRenldmHuod anRenldmHuod = anRenldmHuodService.getActiveHuod();
                if(anRenldmHuod != null) {
                    // 在活动定义的时间范围内才写入
                    if(agRenygj.getZhuapsbbh().equals(anRenldmHuod.getZhuapsxt()) && agRenygj.getZhuapsj().after(anRenldmHuod.getKaissj()) && agRenygj.getZhuapsj().before(anRenldmHuod.getJiessj())) {
                        AgRenldmHuodSign agRenldmHuodSign = new AgRenldmHuodSign();
                        agRenldmHuodSign.setHuod(anRenldmHuod.getId());
                        agRenldmHuodSign.setReny(agRenygj.getRenybh());
                        agRenldmHuodSign.setZhuapsj(agRenygj.getZhuapsj());
                        agRenldmHuodSign.setZhuapsxt(agRenygj.getZhuapsbbh());

                        agRenldmHuodSignService.save(agRenldmHuodSign);
                    }
                }
            }
        });

        yetFinishSync = false;
        return null;
    }

    @Override
    public Object getResult(IPage<AgRenygj> pageList) {
        // 增加records包装
        IPage<RenygjxlGuaidVO> pageList2 = new Page<RenygjxlGuaidVO>(1, 999);

        List <RenygjxlGuaidVO> list = new ArrayList();
        if (pageList != null) {
            ArrayList<AgRenygj> arrayList = new ArrayList<>();
            pageList.getRecords().stream().forEach(e -> {
                arrayList.add(e);
            });

            if (arrayList.size() == 1) {
                //如果只要一个摄像头抓拍到,则返回摄像头详情
                AgRenygj agRenygj1 = arrayList.get(0);
                return agRenygj1;

            }else {
                /*如果多个摄像头抓拍
                 * 1.根据摄像头A和B来匹配 人员轨迹路线 主表
                 * 2.根据主表id去查询从表
                 * */
                for (int i = 0; i < arrayList.size(); i++) {
                    //抓拍设备编号(前)
                    AgRenygj agRenygj1 = arrayList.get(i);
                    String zhuapsbbh1 = agRenygj1.getZhuapsbbh();
                    //抓拍设备编号(后)
                    int j=i;
                    String zhuapsbbh2="";
                    if ((j+1)<arrayList.size()){
                        AgRenygj agRenygj2 = arrayList.get(i+1);
                        zhuapsbbh2 = agRenygj2.getZhuapsbbh();

                    } else {
                        pageList2.setRecords(list);
                        return Result.ok(pageList2);
                    }

                    Map map = new HashMap();
                    map.put("shexta",zhuapsbbh1);
                    map.put("shextb",zhuapsbbh2);
                    Renygjxl renygjxl = renygjxlService.getOne(new QueryWrapper<Renygjxl>().allEq(map));
                    if (renygjxl==null){
                        continue;
                    }
                    String id = renygjxl.getId();
                    String xianlmc = renygjxl.getXianlmc();

                    //得到两点之间的拐点信息
                    List<RenygjxlGuaid> renygjxlGuaidList = renygjxlGuaidService.list(new QueryWrapper<RenygjxlGuaid>().eq("zhubid", id));
                    renygjxlGuaidList.forEach(r -> {
                        RenygjxlGuaidVO renygjxlGuaidVO = new RenygjxlGuaidVO();
                        renygjxlGuaidVO.setCreateBy(r.getCreateBy());
                        renygjxlGuaidVO.setCreateTime(r.getCreateTime());
                        renygjxlGuaidVO.setId(r.getId());
                        renygjxlGuaidVO.setJingd(r.getJingd());
                        renygjxlGuaidVO.setShux(r.getShux());
                        renygjxlGuaidVO.setSysOrgCode(r.getSysOrgCode());
                        renygjxlGuaidVO.setUpdateBy(r.getUpdateBy());
                        renygjxlGuaidVO.setUpdateTime(r.getUpdateTime());
                        renygjxlGuaidVO.setWeid(r.getWeid());
                        renygjxlGuaidVO.setXianlmc(xianlmc);
                        renygjxlGuaidVO.setZhubid(r.getZhubid());

                        list.add(renygjxlGuaidVO);
                    });
                }
            }
        }
        pageList2.setRecords(list);
        return pageList2;
    }
}
